const service = require("../service/manage");
var jwt = require("jsonwebtoken");
const servicearticle = require("../service/article");
// -- 超级管理员登录
const cmsuser = async (ctx) => {
  console.log(ctx.request.body);
  const res = await service.cmsuser(ctx.request.body);
  const ress=await servicearticle.articlecha(ctx.request.body)//管理员登录返回所有文章
  if (res.length > 0) {
    const token = jwt.sign(ctx.request.body, "secret", {
      expiresIn: "24h",
    });
    ctx.body = {
      code: 200,
      message: "登录成功",
      data: { token, res,ress },
    };
  } else {
    ctx.body = {
      code: 403,
      message: "登录失败",
    };
  }
};
// 医生登录
const doctorlogin = async (ctx) => {
  //返给前端的查找的路由表
  const res = await service.doctorFind(ctx.request.body);
  //查找登录
  const ress = await service.doctorlogin(ctx.request.body);
  //医生登录登录返回这个医生的文章
    const resss=await servicearticle.doctorarticle(ctx.request.body)
  if (ress.length > 0) {
    const token = jwt.sign(ctx.request.body, "secret", {
      expiresIn: "24h",
    });
    ctx.body = {
      code: 200,  
      message: "登录成功",
      data: { token, res,resss,ress },
    };
  } else {
    ctx.body = {
      code: 403,
      message: "登录失败",
    };
  }
};
// 管理所有医生--------------------------------------------
const doctorall = async (ctx) => {
  const res = await service.doctorall(ctx.request.body);
  ctx.body = {
    code: 200,
    message: "查找成功",
    data: res,
  };
};
// 管理添加医生
const doctoradd = async (ctx) => {
  const res = await service.doctoradd(ctx.request.body);
  if (res.affectedRows > 0) {
    ctx.body = {
      code: 200,
      message: "添加成功",
    };
  } else {
    ctx.body = {
      code: 403,
      message: "添加失败",
    };
  }
};
// 管理删除医生
const doctordel = async (ctx) => {
  const res = await service.doctordel(ctx.request.body);
  if (res.affectedRows > 0) {
    ctx.body = {
      code: 200,
      message: "删除成功",
    };
  } else {
    ctx.body = {
      code: 403,
      message: "删除失败",
    };
  }
};
// 管理修改医生
const doctorupdate = async (ctx) => {
  const res = await service.doctorupdate(ctx.request.body);

  if (res.affectedRows > 0) {
    ctx.body = {
      code: 200,
      message: "修改成功",
    };
  } else {
    ctx.body = {
      code: 403,
      message: "修改失败",
    };
  }
};
// 管理模糊搜索查找医生
const doctorfind = async (ctx) => {
  const res = await service.doctorfind(ctx.request.body);
  console.log(res);
  if (res.length > 0) {
    ctx.body = {
      code: 200,
      message: "查找成功",
      data: res,
    };
  } else {
    ctx.body = {
      code: 403,
      message: "查找失败",
    };
  }
};
// 审核医生是否同意入驻--------------------------------------------
// 微医申请入驻     // 返回待审核状态
const doctorauditadd = async (ctx) => {
  const res = await service.cha(ctx.request.body);
  if (res.length > 0) {
    ctx.body = {
      code: 403,
      message: "用户名已存在",
    };
  } else {
    const ress = await service.doctorauditadd(ctx.request.body);
    const res = await service.cha(ctx.request.body);
    const data=res[0]
    // 返回待审核状态
    if (ress.affectedRows > 0) {
      ctx.body = {
        code: 200,
        message: "提交成功",
        data: { data },
      };
    } else {
      ctx.body = {
        code: 403,
        message: "提交失败",
      };
    }
  }
};
// 不同意入驻
const doctorauditupdate = async (ctx) => {
  const res = await service.doctorauditupdate(ctx.request.body);
  const ress = await service.cha(ctx.request.body);
    const data=ress[0]
  if (res.affectedRows > 0) {
    ctx.body = {
      code: 200,
      message: "审核未通过",
            data: { data },
    };
  } else {
    ctx.body = {
      code: 403,
      message: "审核失败",
    };
  }
};
// 同意入驻
const doctorauditpass = async (ctx) => {
  const res = await service.doctorauditpass(ctx.request.body);// 审核通过修改状态
  const ress = await service.cha(ctx.request.body);// 返回待审核状态
  const add=await service.doctoradd(ress[0])//调用添加语句
  const data=ress[0]
  if (res.affectedRows > 0) {
    ctx.body = {
      code: 200,
      message: "审核通过",
         data: { data },
    };
  } else {
    ctx.body = {
      code: 403,
      message: "审核失败",
    };
  }
};

// 审核医生删除
const doctorauditdel = async (ctx) => {
  const res = await service.doctorauditdel(ctx.request.body);
  if (res.affectedRows > 0) {
    ctx.body = {
      code: 200,
      message: "删除成功",
    };
  } else {
    ctx.body = {
      code: 403,
      message: "删除失败",
    };
  }
};
//后台审核调用渲染的接口
const doctorauditfind = async (ctx) => {
  const res = await service.doctorauditfind(ctx.request.body);
  if (res.length > 0) {
    ctx.body = {
      code: 200,
      message: "查找成功",
      data: res,
    };
  } else {
    ctx.body = {
      code: 403,
      message: "查找失败",
    };
  }
};
//微医点击查看审核结果
const doctorauditresult = async (ctx) => {
  const res = await service.doctorauditresult(ctx.request.body);
  console.log(res)
  if (res.length > 0) {
    ctx.body = {
      code: 200,
      message: "查找成功",
      data: res,
    };
  } else {
    ctx.body = {
      code: 403,
      message: "查找失败",
    };
  }
  };

module.exports = {
  cmsuser,
  doctorlogin,
  doctoradd,
  doctordel,
  doctorupdate,
  doctorfind,
  doctorall,
  doctorauditadd,
  doctorauditdel,
  doctorauditupdate,
  doctorauditpass,doctorauditfind,doctorauditresult
};
